package com.app.service;

import com.app.model.main.InNotificationRecord;
import com.app.model.response.RetStatus;
import com.app.repository.interfaces.InNotificationRecordMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/7/17. It's a beautiful day.
 */
@Service
public class InNotificationService {

    @Autowired
    private ResponseService responseService;
    @Autowired
    private InNotificationRecordMapper inNotificationRecordMapper;

    public RetStatus addRecord(String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap = null;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int userId = (int)paraMap.get("userId");
        String in = (String)paraMap.get("inTime");
        String out = (String)paraMap.get("outTime");

        long duration = Long.parseLong(out) - Long.parseLong(in);

        InNotificationRecord inNotificationRecord = new InNotificationRecord();
        inNotificationRecord.setUserId((long)userId)
                .setInTime(in)
                .setOutTime(out)
                .setDuration(duration);

        if(inNotificationRecordMapper.insert(inNotificationRecord) == 1){
            return responseService.doResponse(100);
        }else {
            return responseService.doResponse(200);
        }
    }
}
